const crypto = require('crypto')
const prisma = require('./utils/prisma')
const { generateRandomInteger, generateTrainingParams } = require('./common/common')
const { v4: uuidv4 } = require('uuid')
const { Decimal } = require('@prisma/client/runtime/library')

function generateUniqueCode() {
  const data = uuidv4()
  const hash = crypto.createHash('MD5')
  hash.update(data)
  return hash.digest('hex').toLocaleUpperCase()
}

function getRandomElement(array) {
  return array[Math.floor(Math.random() * array.length)]
}

async function main() {
  const material = await prisma.material.findMany({
    select: {
      id: true,
      custom_id: true,
      file_size: true,
      file_type: true
    },
    where: {
      is_del: false
    }
  })
  if (material.length == 0) return

  // 获取AI列表
  const aiList = await prisma.ai_models
    .findMany({
      select: {
        id: true
      }
    })
    .then(res => res.map(item => item.id))

  if (aiList == 0) return

  // 遍历素材添加训练记录
  const reportsRecord = []
  for (let i = 0; i < 100; i++) {
    // 获取随机一个素材
    const randomMaterial = getRandomElement(material)
    const training_time = generateRandomInteger(30, 120)
    const randomInteger = generateRandomInteger(90, 110)
    const oneMaterial = {
      material_id: randomMaterial.id,
      ai_id: aiList[randomMaterial.file_type - 1], // 对应下标取
      training_time,
      size: parseInt((((training_time * 60) / 30) * 124.4 * randomInteger) / 100),
      training_results: getRandomElement(['done', 'doing', 'wait']),
      args: JSON.stringify(generateTrainingParams(randomMaterial.file_type)),
      hit_rate: new Decimal(generateRandomInteger(92, 98)),
      delay: generateRandomInteger(5, 20)
      // TODO 可以模拟创建时间
    }
    reportsRecord.push(oneMaterial)
  }

  // 添加模型训练
  const addTrainReport = await prisma.training_report.createMany({
    data: reportsRecord
  })
  console.log('ok')
}
// main()

const hooks = require('./hooks')
async function test() {
  // hooks.addMaterial(2, 10, 20, new Date('2023-12-01'), new Date('2024-03-14'))
  const t = await prisma.custom_model_relations.createMany({
    data: [
      {
        model_id: 12,
        custom_id: 1000000
      },
      {
        model_id: 12,
        custom_id: 1000002
      }
    ]
  })
  console.log(t)
}
test()
